<?php

namespace App\Livewire\Components;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Component;
use App\Models\Article as ArticleModel;
use Livewire\WithPagination;

class ArchiveArticle extends Component
{
    use WithPagination;

    public $year;

    public function mount($year = false)
    {
        $this->year = $year;
    }

    public function render()
    {
        $year = $this->year ?? Carbon::now()->year;
        $articles = ArticleModel::query()
            ->select(['id', 'category_id', 'title', 'description', 'reading_quantity', 'comment_quantity', 'created_at'])
            ->with('category')
            ->when($year, function (Builder $query, int $year) {
                $query->whereYear('created_at', $year);
            })
            ->paginate(10);
        return view('livewire.components.archive-article', [
            'articles' => $articles,
        ]);
    }
}

